import Vue from 'vue'
import Vuex from 'vuex'
import { DECREMENT } from './mutation-types.js'

import axios from 'axios'

Vue.use(Vuex)

function asyncFun() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(1)
    }, 1000)
  })
}

const state = {
  count: 100,
  hotcate: []
}

const mutations = {
  increment(state, { num }) {
    // console.log(num)
    state.count += num
  },
  async asyncIncrement(state) {
    let result = await asyncFun(state)
    // commit('asyncIncrement', result)
    state.count += result
  },
  [DECREMENT](state, num) {
    state.count -= num
  },
  setHotCate(state, hotcate) {
    state.hotcate = hotcate
  }
}

const actions = {
  async loadHotCate( {commit} ) {
    let result = await axios.get('http://localhost:9000/hotcate')
    commit('setHotCate', result.data.data)
  },

  // async asyncIncrement({commit}) {
  //   let result = await asyncFun(state)
  //   commit('asyncIncrement', result)
  // },

  increment({commit}, num) {
    commit('increment', num)
  }
}

const getters = {
  doubleCount(state) {
    return state.count * 2
  }
}

const store = new Vuex.Store({
  // 状态，data数据
  state,

  // mutations 只能操作同步数据
  mutations,

  // actions 操作异步数据
  actions,

  // getters vuex中的计算属性
  getters
})

// const x = 'aaa'
// const y = {
//   aaa: 100
// }
// console.log(y[x])

export default store